package com.pkcargo.web;

import com.opensymphony.xwork2.ActionSupport;
import com.pkcargo.entity.search.Metadata;
import com.pkcargo.service.search.SearchManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

/**
 * Created by IntelliJ IDEA.
 * User: Howard
 * Date: 2009-4-25
 * Time: 21:13:03
 * To change this template use File | Settings | File Templates.
 */
public class SearchAction extends ActionSupport {
    @Autowired
    private SearchManager searchManager;
    private int type;//货盘=1 运价=2 合作=3
    private String queryString;

    private Page<Metadata> page = new Page<Metadata>(10);

    public Page<Metadata> getPage() {
        return page;
    }

    public void setPage(Page<Metadata> page) {
        this.page = page;
    }

    public String getQueryString() {
        return queryString;
    }

    public void setQueryString(String queryString) {
        this.queryString = queryString;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    @Override
    public String execute() {
        String typeStr = type != 0 ? "type=" + type + ";" : "";
        String limitStr = "limit=" + page.getPageSize() + ";";
        String offsetStr = "offset=" + page.getFirst() + ";";
        String sortStr = "sort=extended:@weight desc, publish_time desc;";
        String query = queryString + ";" + typeStr + limitStr + offsetStr + sortStr + "mode=any";
        String queryCount = queryString+";"+typeStr+"limit=1000;mode=any";
        String sql = "SELECT m.* FROM metadatas m JOIN sphinx ON (m.id=sphinx.id)"
                + "  WHERE query='" + query + "'";
        String countSql = "SELECT count(*) as c FROM sphinx"
                + "  WHERE query='" + queryCount + "'";

        page = searchManager.search(page, sql);
        page.setTotalCount(searchManager.countTotal(countSql));

        return SUCCESS;
    }
}
